Biostat 203B Homework 3

Due Feb 21 @ 11:59PM

Author

Wenqiang Ge UID:106371961

Display machine information for reproducibility:

sessionInfo()
R version 4.4.2 (2024-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
 [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
 [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
[10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

time zone: America/Los_Angeles
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] htmlwidgets_1.6.4 compiler_4.4.2    fastmap_1.2.0     cli_3.6.3        
 [5] tools_4.4.2       htmltools_0.5.8.1 rstudioapi_0.17.1 yaml_2.3.10      
 [9] rmarkdown_2.29    knitr_1.49        jsonlite_1.8.9    xfun_0.50        
[13] digest_0.6.37     rlang_1.1.5       evaluate_1.0.3   

Load necessary libraries (you can add more as needed).

# Load necessary libraries
library(arrow)

Attaching package: 'arrow'
The following object is masked from 'package:utils':

    timestamp
library(gtsummary)
library(memuse)
library(pryr)

Attaching package: 'pryr'
The following object is masked from 'package:gtsummary':

    where
library(R.utils)
Loading required package: R.oo
Loading required package: R.methodsS3
R.methodsS3 v1.8.2 (2022-06-13 22:00:14 UTC) successfully loaded. See ?R.methodsS3 for help.
R.oo v1.27.0 (2024-11-01 18:00:02 UTC) successfully loaded. See ?R.oo for help.

Attaching package: 'R.oo'
The following object is masked from 'package:R.methodsS3':

    throw
The following objects are masked from 'package:methods':

    getClasses, getMethods
The following objects are masked from 'package:base':

    attach, detach, load, save
R.utils v2.12.3 (2023-11-18 01:00:02 UTC) successfully loaded. See ?R.utils for help.

Attaching package: 'R.utils'
The following object is masked from 'package:arrow':

    timestamp
The following object is masked from 'package:utils':

    timestamp
The following objects are masked from 'package:base':

    cat, commandArgs, getOption, isOpen, nullfile, parse, use, warnings
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ purrr::compose()      masks pryr::compose()
✖ lubridate::duration() masks arrow::duration()
✖ tidyr::extract()      masks R.utils::extract()
✖ dplyr::filter()       masks stats::filter()
✖ dplyr::lag()          masks stats::lag()
✖ purrr::partial()      masks pryr::partial()
✖ dplyr::where()        masks pryr::where(), gtsummary::where()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(data.table)

Attaching package: 'data.table'

The following objects are masked from 'package:lubridate':

    hour, isoweek, mday, minute, month, quarter, second, wday, week,
    yday, year

The following objects are masked from 'package:dplyr':

    between, first, last

The following object is masked from 'package:purrr':

    transpose

The following object is masked from 'package:pryr':

    address
library(duckdb)
Loading required package: DBI
library(memuse)
library(ggplot2)
library(dplyr)
library(lubridate)
library(readr)
library(duckdb)
library(DBI)

Display your machine memory.

memuse::Sys.meminfo()
Totalram:  7.686 GiB 
Freeram:   5.478 GiB 

In this exercise, we use tidyverse (ggplot2, dplyr, etc) to explore the MIMIC-IV data introduced in homework 1 and to build a cohort of ICU stays.

Q1. Visualizing patient trajectory

Visualizing a patient’s encounters in a health care system is a common task in clinical data analysis. In this question, we will visualize a patient’s ADT (admission-discharge-transfer) history and ICU vitals in the MIMIC-IV data.

Q1.1 ADT history

A patient’s ADT history records the time of admission, discharge, and transfer in the hospital. This figure shows the ADT history of the patient with subject_id 10001217 in the MIMIC-IV data. The x-axis is the calendar time, and the y-axis is the type of event (ADT, lab, procedure). The color of the line segment represents the care unit. The size of the line segment represents whether the care unit is an ICU/CCU. The crosses represent lab events, and the shape of the dots represents the type of procedure. The title of the figure shows the patient’s demographic information and the subtitle shows top 3 diagnoses.

Do a similar visualization for the patient with subject_id 10063848 using ggplot.

Hint: We need to pull information from data files patients.csv.gz, admissions.csv.gz, transfers.csv.gz, labevents.csv.gz, procedures_icd.csv.gz, diagnoses_icd.csv.gz, d_icd_procedures.csv.gz, and d_icd_diagnoses.csv.gz. For the big file labevents.csv.gz, use the Parquet format you generated in Homework 2. For reproducibility, make the Parquet folder labevents_pq available at the current working directory hw3, for example, by a symbolic link. Make your code reproducible.


Solution:

# Load datasets
patients <- read_csv("~/mimic/hosp/patients.csv.gz")
admissions <- read_csv("~/mimic/hosp/admissions.csv.gz")
transfers <- read_csv("~/mimic/hosp/transfers.csv.gz")

procedures <- read_csv("~/mimic/hosp/procedures_icd.csv.gz")
diagnoses <- read_csv("~/mimic/hosp/diagnoses_icd.csv.gz")
d_icd_procedures <- read_csv("~/mimic/hosp/d_icd_procedures.csv.gz")
d_icd_diagnoses <- read_csv("~/mimic/hosp/d_icd_diagnoses.csv.gz")

labevents <- open_dataset(
  "~/biostat-203b-2025-winter/hw3/labevents_parquet",
  format = "parquet"
)
# Filter for patient 10063848
patient_id <- 10063848
patient_data <- patients %>% filter(subject_id == patient_id)
admissions_data <- admissions %>% filter(subject_id == patient_id)
transfers_data <- transfers %>% filter(subject_id == patient_id)
labevents_data <- labevents %>% filter(subject_id == patient_id)
procedures_data <- procedures %>% filter(subject_id == patient_id)
diagnoses_data <- diagnoses %>% filter(subject_id == patient_id)

rm(patients,admissions,transfers,labevents,
   procedures,d_icd_procedures,d_icd_diagnoses)

Q1.2 ICU stays

ICU stays are a subset of ADT history. This figure shows the vitals of the patient 10001217 during ICU stays. The x-axis is the calendar time, and the y-axis is the value of the vital. The color of the line represents the type of vital. The facet grid shows the abbreviation of the vital and the stay ID.

Do a similar visualization for the patient 10063848.


Solution:

Q2. ICU stays

icustays.csv.gz (https://mimic.mit.edu/docs/iv/modules/icu/icustays/) contains data about Intensive Care Units (ICU) stays. The first 10 lines are

zcat < ~/mimic/icu/icustays.csv.gz | head
subject_id,hadm_id,stay_id,first_careunit,last_careunit,intime,outtime,los
10000032,29079034,39553978,Medical Intensive Care Unit (MICU),Medical Intensive Care Unit (MICU),2180-07-23 14:00:00,2180-07-23 23:50:47,0.4102662037037037
10000690,25860671,37081114,Medical Intensive Care Unit (MICU),Medical Intensive Care Unit (MICU),2150-11-02 19:37:00,2150-11-06 17:03:17,3.8932523148148146
10000980,26913865,39765666,Medical Intensive Care Unit (MICU),Medical Intensive Care Unit (MICU),2189-06-27 08:42:00,2189-06-27 20:38:27,0.4975347222222222
10001217,24597018,37067082,Surgical Intensive Care Unit (SICU),Surgical Intensive Care Unit (SICU),2157-11-20 19:18:02,2157-11-21 22:08:00,1.1180324074074075
10001217,27703517,34592300,Surgical Intensive Care Unit (SICU),Surgical Intensive Care Unit (SICU),2157-12-19 15:42:24,2157-12-20 14:27:41,0.948113425925926
10001725,25563031,31205490,Medical/Surgical Intensive Care Unit (MICU/SICU),Medical/Surgical Intensive Care Unit (MICU/SICU),2110-04-11 15:52:22,2110-04-12 23:59:56,1.338587962962963
10001843,26133978,39698942,Medical/Surgical Intensive Care Unit (MICU/SICU),Medical/Surgical Intensive Care Unit (MICU/SICU),2134-12-05 18:50:03,2134-12-06 14:38:26,0.8252662037037037
10001884,26184834,37510196,Medical Intensive Care Unit (MICU),Medical Intensive Care Unit (MICU),2131-01-11 04:20:05,2131-01-20 08:27:30,9.17181712962963
10002013,23581541,39060235,Cardiac Vascular Intensive Care Unit (CVICU),Cardiac Vascular Intensive Care Unit (CVICU),2160-05-18 10:00:53,2160-05-19 17:33:33,1.314351851851852

Q2.1 Ingestion

Import icustays.csv.gz as a tibble icustays_tble.


Solution:

icustays_tble <- read_csv("~/mimic/icu/icustays.csv.gz")
Rows: 94458 Columns: 8
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (2): first_careunit, last_careunit
dbl  (4): subject_id, hadm_id, stay_id, los
dttm (2): intime, outtime

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Q2.2 Summary and visualization

How many unique subject_id? Can a subject_id have multiple ICU stays? Summarize the number of ICU stays per subject_id by graphs.


Solution:

# Count unique subject_id
num_unique_subjects <- icustays_tble%>% 
  distinct(subject_id) %>% 
  nrow()

print(paste("Number of unique subjects:", num_unique_subjects))
[1] "Number of unique subjects: 65366"
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:arrow':

    schema
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
# Count ICU stays per subject
icu_stays_per_subject <- icustays_tble %>%
  group_by(subject_id) %>%
  summarise(num_stays = n(), .groups = "drop")

# Create a histogram to visualize the distribution
p <- ggplot(icu_stays_per_subject, aes(x = num_stays)) +
  geom_histogram(binwidth = 1, fill = "#1B9E76", color = "black", alpha = 0.7) +  # Use histogram with bin width 1
  scale_x_continuous(limits = c(0, 30)) +  # Limit X-axis to focus on the majority of data
  labs(title = "Distribution of ICU Stays per Subject",
       x = "Number of ICU Stays",
       y = "Count of Subjects") +
  theme_minimal()  # Use a clean theme for better readability

# Convert to an interactive plot
ggplotly(p)
Warning: Removed 4 rows containing non-finite outside the scale range
(`stat_bin()`).
rm(num_unique_subjects)

The histogram shows the distribution of ICU stays per subject (patient). Most subjects have only one ICU stay, with a sharp drop-off as the number of stays increases. The majority of patients (49,124 subjects) had a single ICU stay, while a much smaller proportion experienced multiple ICU stays. This suggests that repeated ICU admissions are uncommon, but some patients do return multiple times, possibly due to chronic conditions, post-surgical complications, or severe illnesses requiring multiple admissions. The long tail of the distribution indicates that a few patients had 10 or more ICU stays, though these cases are rare.

Q3. admissions data

Information of the patients admitted into hospital is available in admissions.csv.gz. See https://mimic.mit.edu/docs/iv/modules/hosp/admissions/ for details of each field in this file. The first 10 lines are

zcat < ~/mimic/hosp/admissions.csv.gz | head
subject_id,hadm_id,admittime,dischtime,deathtime,admission_type,admit_provider_id,admission_location,discharge_location,insurance,language,marital_status,race,edregtime,edouttime,hospital_expire_flag
10000032,22595853,2180-05-06 22:23:00,2180-05-07 17:15:00,,URGENT,P49AFC,TRANSFER FROM HOSPITAL,HOME,Medicaid,English,WIDOWED,WHITE,2180-05-06 19:17:00,2180-05-06 23:30:00,0
10000032,22841357,2180-06-26 18:27:00,2180-06-27 18:49:00,,EW EMER.,P784FA,EMERGENCY ROOM,HOME,Medicaid,English,WIDOWED,WHITE,2180-06-26 15:54:00,2180-06-26 21:31:00,0
10000032,25742920,2180-08-05 23:44:00,2180-08-07 17:50:00,,EW EMER.,P19UTS,EMERGENCY ROOM,HOSPICE,Medicaid,English,WIDOWED,WHITE,2180-08-05 20:58:00,2180-08-06 01:44:00,0
10000032,29079034,2180-07-23 12:35:00,2180-07-25 17:55:00,,EW EMER.,P06OTX,EMERGENCY ROOM,HOME,Medicaid,English,WIDOWED,WHITE,2180-07-23 05:54:00,2180-07-23 14:00:00,0
10000068,25022803,2160-03-03 23:16:00,2160-03-04 06:26:00,,EU OBSERVATION,P39NWO,EMERGENCY ROOM,,,English,SINGLE,WHITE,2160-03-03 21:55:00,2160-03-04 06:26:00,0
10000084,23052089,2160-11-21 01:56:00,2160-11-25 14:52:00,,EW EMER.,P42H7G,WALK-IN/SELF REFERRAL,HOME HEALTH CARE,Medicare,English,MARRIED,WHITE,2160-11-20 20:36:00,2160-11-21 03:20:00,0
10000084,29888819,2160-12-28 05:11:00,2160-12-28 16:07:00,,EU OBSERVATION,P35NE4,PHYSICIAN REFERRAL,,Medicare,English,MARRIED,WHITE,2160-12-27 18:32:00,2160-12-28 16:07:00,0
10000108,27250926,2163-09-27 23:17:00,2163-09-28 09:04:00,,EU OBSERVATION,P40JML,EMERGENCY ROOM,,,English,SINGLE,WHITE,2163-09-27 16:18:00,2163-09-28 09:04:00,0
10000117,22927623,2181-11-15 02:05:00,2181-11-15 14:52:00,,EU OBSERVATION,P47EY8,EMERGENCY ROOM,,Medicaid,English,DIVORCED,WHITE,2181-11-14 21:51:00,2181-11-15 09:57:00,0

Q3.1 Ingestion

Import admissions.csv.gz as a tibble admissions_tble.


Solution:

admissions_tble <- read_csv("~/mimic/hosp/admissions.csv.gz")
Rows: 546028 Columns: 16
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (8): admission_type, admit_provider_id, admission_location, discharge_l...
dbl  (3): subject_id, hadm_id, hospital_expire_flag
dttm (5): admittime, dischtime, deathtime, edregtime, edouttime

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Q3.2 Summary and visualization

Summarize the following information by graphics and explain any patterns you see.

  • number of admissions per patient
  • admission hour (anything unusual?)
  • admission minute (anything unusual?)
  • length of hospital stay (from admission to discharge) (anything unusual?)

According to the MIMIC-IV documentation,

All dates in the database have been shifted to protect patient confidentiality. Dates will be internally consistent for the same patient, but randomly distributed in the future. Dates of birth which occur in the present time are not true dates of birth. Furthermore, dates of birth which occur before the year 1900 occur if the patient is older than 89. In these cases, the patient’s age at their first admission has been fixed to 300.


Solution:

  • number of admissions per patient
# Compute the number of admissions per patient
admissions_per_patient <- admissions_tble %>%
  group_by(subject_id) %>%
  summarise(num_admissions = n(), .groups = "drop")

# Create a histogram to visualize the distribution
p <- ggplot(admissions_per_patient, aes(x = num_admissions)) +
  geom_histogram(binwidth = 1, fill = "#D81B60", color = "black", alpha = 0.8) + # Use histogram instead of bar chart
  scale_x_continuous(limits = c(0, 30)) +  # Limit the X-axis to focus on the most relevant range
  labs(title = "Number of Admissions per Patient",
       x = "Admissions per Patient",
       y = "Count of Patients") +
  theme_minimal()  # Use a clean theme for better readability

# Convert to an interactive plot
ggplotly(p)
Warning: Removed 504 rows containing non-finite outside the scale range
(`stat_bin()`).
rm(admissions_per_patient)

Most patients have only one or very few admissions, while very few patients have a large number of admissions. A small number of patients have multiple hospitalizations, but the frequency drops quickly as the number of admissions increases. Patients with 10+ admissions are very rare. The distribution is heavily right-skewed, with very few patients experiencing more than 5 hospital stays. This means that most patients have a low number of admissions, and a small subset of patients has a significantly higher number. Also, Most hospital patients are one-time visitors, while a minority (likely with chronic illnesses or complications) are readmitted multiple times.

  • admission hour (anything unusual?)
# Extract hour from admission time
admissions_tble <- admissions_tble %>%
  mutate(admission_hour = hour(admittime))

# Create a histogram of admission hours
p_hour <- ggplot(admissions_tble, aes(x = admission_hour)) +
  geom_histogram(binwidth = 1, fill = "#1E88E5", color = "black", alpha = 0.8) +
  scale_x_continuous(breaks = seq(0, 23, by = 1)) +
  labs(title = "Distribution of Admission Hours",
       x = "Hour of Admission",
       y = "Count of Admissions") +
  theme_minimal()

# Convert to interactive plot
ggplotly(p_hour)

The distribution of admission hours shows notable patterns. There is a sharp peak at midnight (00:00), suggesting that many admissions are recorded at the start of the day, possibly due to administrative rounding or batch processing of records. Another smaller peak is observed around 07:00–08:00, which could be associated with morning shift changes or scheduled hospital admissions. Admissions increase steadily from late morning and peak between 16:00–19:00, likely reflecting patient arrivals after daytime medical consultations and emergency cases during evening hours. The pattern indicates a combination of systemic recording practices, scheduled admissions, and emergency patient arrivals.

  • admission minute (anything unusual?)
# Extract minute from admission time
admissions_tble <- admissions_tble %>%
  mutate(admission_minute = minute(admittime))

# Create a histogram of admission minutes
p_minute <- ggplot(admissions_tble, aes(x = admission_minute)) +
  geom_histogram(binwidth = 1, fill = "#FFC107", color = "black", alpha = 0.8) +
  scale_x_continuous(breaks = seq(0, 59, by = 5)) +
  labs(title = "Distribution of Admission Minutes",
       x = "Minute of Admission",
       y = "Count of Admissions") +
  theme_minimal()

# Convert to interactive plot
ggplotly(p_minute)

The distribution of admission minutes shows distinct spikes at 0, 15, 30, and 45 minutes, suggesting that admission times are often rounded to quarter-hour intervals. This pattern likely arises due to manual data entry practices, hospital system constraints, or administrative processes that batch-record admissions. The relatively uniform distribution in other minutes indicates that some admissions occur naturally without rounding, but the spikes highlight a systemic bias in how times are logged. This could impact time-based analyses and should be accounted for in further data processing.

  • length of hospital stay (from admission to discharge) (anything unusual?)
# Calculate length of stay in days
admissions_tble <- admissions_tble %>%
  mutate(length_of_stay = as.numeric(difftime(dischtime, admittime, units = "days")))

# Create a histogram of hospital stay length
p_los <- ggplot(admissions_tble, aes(x = length_of_stay)) +
  geom_histogram(binwidth = 1, fill = "#4CAF50", color = "black", alpha = 0.8) +
  scale_x_continuous(limits = c(0, 50)) +  # Adjusted limit for visualization
  labs(title = "Distribution of Hospital Stay Length",
       x = "Length of Stay (Days)",
       y = "Count of Admissions") +
  theme_minimal()

# Convert to interactive plot
ggplotly(p_los)
Warning: Removed 2105 rows containing non-finite outside the scale range
(`stat_bin()`).

The distribution of hospital stay length is right-skewed, with most admissions having a short length of stay. The majority of patients are discharged within a few days, with the highest count around 1–3 days, suggesting that many hospital visits are for short-term treatments or observations. As the length of stay increases, the number of admissions decreases, indicating that extended hospitalizations are less common. The presence of a long tail suggests that a small number of patients require prolonged hospital care, possibly due to severe or chronic conditions. This pattern is typical in hospital data, where most cases are routine and resolved quickly, while complex cases require extended stays.

Q4. patients data

Patient information is available in patients.csv.gz. See https://mimic.mit.edu/docs/iv/modules/hosp/patients/ for details of each field in this file. The first 10 lines are

zcat < ~/mimic/hosp/patients.csv.gz | head

Q4.1 Ingestion

Import patients.csv.gz (https://mimic.mit.edu/docs/iv/modules/hosp/patients/) as a tibble patients_tble.


Solution:

patients_tble <- read_csv("~/mimic/hosp/patients.csv.gz")
Rows: 364627 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (2): gender, anchor_year_group
dbl  (3): subject_id, anchor_age, anchor_year
date (1): dod

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Q4.2 Summary and visualization

Summarize variables gender and anchor_age by graphics, and explain any patterns you see.


Solution:

# Count the number of patients by gender
gender_dist <- patients_tble %>%
  count(gender)

# Bar plot for gender distribution
p_gender <- ggplot(gender_dist, aes(x = gender, y = n, fill = gender)) +
  geom_bar(stat = "identity", color = "black", alpha = 0.8) +
  labs(title = "Gender Distribution",
       x = "Gender",
       y = "Count of Patients") +
  theme_minimal()

# Convert to interactive plot
ggplotly(p_gender)
rm(gender_dist)

The bar chart indicates that there are more female (F) patients than male (M) patients in the dataset. The difference is noticeable but not extreme, suggesting a slight gender imbalance in hospital admissions. This could be influenced by factors such as longer life expectancy in females, higher healthcare utilization rates among women, or specific hospital demographics.

# Histogram of anchor_age
p_age <- ggplot(patients_tble, aes(x = anchor_age)) +
  geom_histogram(binwidth = 5, fill = "#1E88E5", color = "black", alpha = 0.8) +
  labs(title = "Distribution of Anchor Age",
       x = "Age (Years)",
       y = "Count of Patients") +
  theme_minimal()

# Convert to interactive plot
ggplotly(p_age)

The histogram of anchor_age shows a bimodal distribution, with a large concentration of patients in the 20–30 age range and another broader peak between 50–70 years old. The younger peak could represent a large number of maternity-related or younger adult admissions, while the older peak likely reflects age-related chronic conditions and elderly care. The drop-off after 80 years suggests a lower number of very elderly patients, possibly due to lower life expectancy or selection bias in hospital records.

Q5. Lab results

labevents.csv.gz (https://mimic.mit.edu/docs/iv/modules/hosp/labevents/) contains all laboratory measurements for patients. The first 10 lines are

zcat < ~/mimic/hosp/labevents.csv.gz | head

d_labitems.csv.gz (https://mimic.mit.edu/docs/iv/modules/hosp/d_labitems/) is the dictionary of lab measurements.

zcat < ~/mimic/hosp/d_labitems.csv.gz | head

We are interested in the lab measurements of creatinine (50912), potassium (50971), sodium (50983), chloride (50902), bicarbonate (50882), hematocrit (51221), white blood cell count (51301), and glucose (50931). Retrieve a subset of labevents.csv.gz that only containing these items for the patients in icustays_tble. Further restrict to the last available measurement (by storetime) before the ICU stay. The final labevents_tble should have one row per ICU stay and columns for each lab measurement.

Hint: Use the Parquet format you generated in Homework 2. For reproducibility, make labevents_pq folder available at the current working directory hw3, for example, by a symbolic link.


Solution:

# Connect to DuckDB
con <- dbConnect(duckdb::duckdb(), dbdir = ":memory:")

# Load and filter lab events directly in DuckDB
dbExecute(con, "CREATE TABLE filtered_labevents AS 
                SELECT subject_id, itemid, storetime, valuenum
                FROM read_parquet('~/biostat-203b-2025-winter/hw3/labevents_parquet/part-0.parquet')
                WHERE itemid IN (50912, 50971, 50983, 50902, 50882, 51221, 51301, 50931)")
[1] 32679896
#  Load ICU stays dataset from CSV and store it in DuckDB
dbExecute(con, "CREATE TABLE icustays AS 
                SELECT subject_id, stay_id, intime, outtime 
                FROM read_csv_auto('~/mimic/icu/icustays.csv.gz')")
[1] 94458
# Join lab events with ICU stays and filter by time condition
dbExecute(con, "CREATE TABLE merged_labevents AS 
                SELECT l.subject_id, i.stay_id, l.itemid, l.storetime, l.valuenum
                FROM filtered_labevents l
                INNER JOIN icustays i ON l.subject_id = i.subject_id
                WHERE l.storetime < i.intime")
[1] 20122551
# Select the most recent lab measurement per `itemid` before ICU admission
dbExecute(con, "CREATE TABLE latest_labs AS 
                SELECT subject_id, stay_id, itemid, storetime, valuenum
                FROM (
                    SELECT *, ROW_NUMBER() OVER (PARTITION BY subject_id, stay_id, itemid ORDER BY storetime DESC) AS rn
                    FROM merged_labevents
                ) WHERE rn = 1")
[1] 677237
# Pivot `itemid` to become separate columns
dbExecute(con, "CREATE TABLE labevents_pivoted AS 
                SELECT subject_id, stay_id,
                       MAX(CASE WHEN itemid = 50882 THEN valuenum END) AS bicarbonate,
                       MAX(CASE WHEN itemid = 50902 THEN valuenum END) AS chloride,
                       MAX(CASE WHEN itemid = 50912 THEN valuenum END) AS creatinine,
                       MAX(CASE WHEN itemid = 50931 THEN valuenum END) AS glucose,
                       MAX(CASE WHEN itemid = 50971 THEN valuenum END) AS potassium,
                       MAX(CASE WHEN itemid = 50983 THEN valuenum END) AS sodium,
                       MAX(CASE WHEN itemid = 51221 THEN valuenum END) AS hematocrit,
                       MAX(CASE WHEN itemid = 51301 THEN valuenum END) AS wbc
                FROM latest_labs
                GROUP BY subject_id, stay_id")
[1] 88086
# Fetch the final processed table into R
labevents_tble <- dbGetQuery(con, "SELECT * FROM labevents_pivoted")

# Arrange for better readability
labevents_tble <- labevents_tble %>% arrange(subject_id, stay_id)

# View final dataset
print(head(labevents_tble, 10), width = 250)
   subject_id  stay_id bicarbonate chloride creatinine glucose potassium sodium hematocrit  wbc
1    10000032 39553978          25       95        0.7     102       6.7    126       41.1  6.9
2    10000690 37081114          26      100        1.0      85       4.8    137       36.1  7.1
3    10000980 39765666          21      109        2.3      89       3.9    144       27.3  5.3
4    10001217 34592300          30      104        0.5      87       4.1    142       37.4  5.4
5    10001217 37067082          22      108        0.6     112       4.2    142       38.1 15.7
6    10001725 31205490          NA       98         NA      NA       4.1    139         NA   NA
7    10001843 39698942          28       97        1.3     131       3.9    138       31.4 10.4
8    10001884 37510196          30       88        1.1     141       4.5    130       39.7 12.2
9    10002013 39060235          24      102        0.9     288       3.5    137       34.9  7.2
10   10002114 34672098          18       NA        3.1      95       6.5    125       34.3 16.8
# Disconnect from DuckDB
dbDisconnect(con, shutdown = TRUE)

rm(con)

Q6. Vitals from charted events

chartevents.csv.gz (https://mimic.mit.edu/docs/iv/modules/icu/chartevents/) contains all the charted data available for a patient. During their ICU stay, the primary repository of a patient’s information is their electronic chart. The itemid variable indicates a single measurement type in the database. The value variable is the value measured for itemid. The first 10 lines of chartevents.csv.gz are

zcat < ~/mimic/icu/chartevents.csv.gz | head

d_items.csv.gz (https://mimic.mit.edu/docs/iv/modules/icu/d_items/) is the dictionary for the itemid in chartevents.csv.gz.

zcat < ~/mimic/icu/d_items.csv.gz | head

We are interested in the vitals for ICU patients: heart rate (220045), systolic non-invasive blood pressure (220179), diastolic non-invasive blood pressure (220180), body temperature in Fahrenheit (223761), and respiratory rate (220210). Retrieve a subset of chartevents.csv.gz only containing these items for the patients in icustays_tble. Further restrict to the first vital measurement within the ICU stay. The final chartevents_tble should have one row per ICU stay and columns for each vital measurement.

Hint: Use the Parquet format you generated in Homework 2. For reproducibility, make chartevents_pq folder available at the current working directory, for example, by a symbolic link.


Solution:

#Connect to DuckDB
con <- dbConnect(duckdb::duckdb(), dbdir = ":memory:")

# Load ICU stays dataset from CSV into DuckDB
duckdb_table_icustays <- tbl(con, sql("SELECT * FROM read_csv_auto('~/mimic/icu/icustays.csv.gz')"))

# Query ICU stays dataset within DuckDB
result_icustays <- duckdb_table_icustays %>%
  select(subject_id, stay_id, intime,outtime) %>%  # Select necessary columns
  collect()  # Bring the filtered results into memory as a tibble
# Connect to DuckDB
con <- dbConnect(duckdb::duckdb(), dbdir = ":memory:") 

#  Load and filter only required `itemid` values (Reduce Memory Usage)
dbExecute(con, "CREATE TABLE chartevents_duckdb AS 
                SELECT subject_id, itemid, storetime, valuenum 
                FROM read_parquet('~/biostat-203b-2025-winter/hw3/chartevents_parquet/part-0.parquet')
                WHERE itemid IN (220045, 220179, 220180, 223761, 220210)")
[1] 30200193
#  Load `icustays_tble` into DuckDB
dbWriteTable(con, "icustays", result_icustays, overwrite = TRUE)
# Filter the measurements in between the icu time
dbExecute(con, "CREATE TABLE latest_vitals_raw AS 
                SELECT c.subject_id, i.stay_id, c.itemid, c.storetime, c.valuenum
                FROM chartevents_duckdb c
                INNER JOIN icustays i 
                ON c.subject_id = i.subject_id
                WHERE c.storetime BETWEEN i.intime AND i.outtime")
[1] 30129155
# Select the first measurement (`storetime`) per `itemid` and `stay_id`
dbExecute(con, "CREATE TABLE latest_vitals_filtered AS 
                SELECT subject_id, stay_id, itemid, storetime, valuenum
                FROM (
                    SELECT *, 
                    ROW_NUMBER() OVER 
                    (PARTITION BY subject_id, stay_id, itemid 
                    ORDER BY storetime ASC) AS rn
                    FROM latest_vitals_raw
                ) WHERE rn = 1")
# Pivot `itemid` into separate columns (one row per ICU stay)
latest_vitals <- dbGetQuery(con, "SELECT * FROM latest_vitals_filtered")

chartevents_tble <- latest_vitals %>%
  select(-storetime) %>%
  pivot_wider(names_from = itemid, values_from = valuenum, values_fill = list(valuenum = NA))

# Create a named vector mapping `itemid` to readable names
itemid_mapping <- c(
  "220045" = "heart_rate",
  "220179" = "non-invasive_blood_pressure_systolic",
  "220180" = "non-invasive_blood_pressure_diastolic",
  "223761" = "temperature_fahrenheit",
  "220210" = "respiratory_rate"
)

# Rename columns based on `itemid_mapping`
colnames(chartevents_tble) <- c("subject_id", "stay_id", 
                                itemid_mapping[colnames(chartevents_tble)[-(1:2)]]) 

rm(latest_vitals)
# Arrange for better readability
chartevents_tble <- chartevents_tble %>% arrange(subject_id, stay_id)

# View the final dataset
print(head(chartevents_tble, 10), width = 270)

# disconnect from DuckDB
dbDisconnect(con, shutdown = TRUE)

Q7. Putting things together

Let us create a tibble mimic_icu_cohort for all ICU stays, where rows are all ICU stays of adults (age at intime >= 18) and columns contain at least following variables

  • all variables in icustays_tble
  • all variables in admissions_tble
  • all variables in patients_tble
  • the last lab measurements before the ICU stay in labevents_tble
  • the first vital measurements during the ICU stay in chartevents_tble

The final mimic_icu_cohort should have one row per ICU stay and columns for each variable.


Solution:

Q8. Exploratory data analysis (EDA)

Summarize the following information about the ICU stay cohort mimic_icu_cohort using appropriate numerics or graphs:

  • Length of ICU stay los vs demographic variables (race, insurance, marital_status, gender, age at intime)

  • Length of ICU stay los vs the last available lab measurements before ICU stay

  • Length of ICU stay los vs the first vital measurements within the ICU stay

  • Length of ICU stay los vs first ICU unit